diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py
index 2b306a579e..352b083234 100644
--- a/django/core/management/commands/shell.py
+++ b/django/core/management/commands/shell.py
@@ -84,13 +84,13 @@ class Command(BaseCommand):
     def handle(self, **options):
         # Execute the command and exit.
         if options['command']:
-            exec(options['command'])
+            exec(options['command'], {'__builtins__': __builtins__})
             return
 
         # Execute stdin if it has anything to read and exit.
         # Not supported on Windows due to select.select() limitations.
         if sys.platform != 'win32' and not sys.stdin.isatty() and select.select([sys.stdin], [], [], 0)[0]:
-            exec(sys.stdin.read())
+            exec(sys.stdin.read(), {'__builtins__': __builtins__})
             return
 
         available_shells = [options['interface']] if options['interface'] else self.shells
